using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DataAccessLayer;
using WebUI.Custom_Cotrols;

namespace WebUI.ManagerPages
{
    public partial class EditJob : System.Web.UI.Page
    {
        private DataSet _jobData;
        private DataRow _job;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            btnAddTask.Visible = false;

            if (Request.QueryString["JobID"] == null)
            {
                lblJobID.Text="Create New Job";
            }
            else
            {
                int jobID = int.Parse(Request.QueryString["JobID"]);
                lblJobID.Text= "Details for Job # " + jobID;
                if (DAL.GetJobData((int)(Session[Keys.Session.UserID]), int.Parse(Request.QueryString["JobID"]),out _jobData))
                {
                    _job = _jobData.Tables["Job"].Rows[0];
                    lblCustomerName.Text = _job.ItemArray[2].ToString();
                    if (!Page.IsPostBack)
                    {
                        txtJobDescription.Text = _job.ItemArray[3].ToString();
                        foreach (ListItem li in ddlBillingMethod.Items)
                        {
                            if (int.Parse(li.Value) == (int)_job.ItemArray[4])
                            {
                                ddlBillingMethod.SelectedIndex = int.Parse(li.Value) - 1;
                                break;
                            }
                        }
                        chckbxJobFinalized.Checked = (bool)_job.ItemArray[6];
                        if (_job.ItemArray[7] != DBNull.Value)
                        {
                            lblJobDueDate.Text = ((DateTime)_job.ItemArray[7]).ToShortDateString();
                        }
                        else
                        {
                            lblJobDueDate.Text = "N/A";
                        }
                        lblJobCreated.Text = ((DateTime)_job.ItemArray[8]).ToShortDateString();
                        lblJobModified.Text = ((DateTime)_job.ItemArray[9]).ToShortDateString();
                        DataTable users = _jobData.Tables["User"];

                        lstbxUsers.Items.Clear();

                        foreach (DataRow user in users.Rows)
                        {
                            lstbxUsers.Items.Add(new ListItem(user.ItemArray[1].ToString()));
                        }
                        DataTable tasks = _jobData.Tables[2];
                        foreach (DataRow task in tasks.Rows)
                        {
                            pnlTasks.Controls.Add(new TaskControl(task, (int)_job.ItemArray[1], (int)Session[Keys.Session.UserID]));
                        } 
                    }
                }
                else
                {
                    this.Controls.Clear();
                    Label error = new Label();
                    error.Text = "There was a Problem Retrieving the Data from the Database";
                    error.Font.Bold = true;
                    error.Font.Size = new FontUnit(FontSize.Large);
                }
            }
        }

        protected void btnAddTask_Click(object sender, EventArgs e)
        {
            DataTable tasks = _jobData.Tables[2];
            foreach (DataRow task in tasks.Rows)
            {
                pnlTasks.Controls.Add(new TaskControl(task,(int)_job.ItemArray[1],(int)Session[Keys.Session.UserID]));

            }

        }    

        protected void btnSaveTask_Click(object sender, EventArgs e)
        {
        
        }

        protected void btnAddUser_Click(object sender, EventArgs e)
        {
            if (lstbxUsers.Items.ToString().Contains(""))
            {
                lstbxUsers.Items.Add(Keys.Session.UserName);
            
            }

            else if (lstbxUsers.Items.ToString().Contains("Administrator"))
            {
                lstbxUsers.Items.Add(Keys.Session.UserName);                
            
            }

        }

        protected void btnSaveJob_Click(object sender, EventArgs e)
        {
            //03469
            _job[3] = txtJobDescription.Text;
            DAL.UpdateJob((int)Session[Keys.Session.UserID], _job);
            btnAddTask.Visible = true;
        }
    }
}
